package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.SeasonCompetionTeamDao;
import cz.khfotbal.domain.Season;
import cz.khfotbal.domain.SeasonCompetion;
import cz.khfotbal.domain.SeasonCompetionTeam;
import cz.khfotbal.domain.Team;

@Repository
@Transactional
public class SeasonCompetionTeamDaoImpl implements SeasonCompetionTeamDao {

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;

//	public List<SeasonCompetionTeam> findBySeasonCompetion(int seasonCompetionId) {
//		session = sessionFactory.getCurrentSession();
//		List<SeasonCompetionTeam> result = session.createQuery(
//				" from SeasonCompetionTeam sct where sct.seasonCompetion.id="
//						+ seasonCompetionId + " order by number").list();
//		return result;
//	}
	
	public List<SeasonCompetionTeam> findBySeasonCompetion(int seasonCompetionId) {
		session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(
				" from SeasonCompetionTeam sct where sct.seasonCompetion.id="
						+ seasonCompetionId + " order by number");
		query.setCacheable(true);
		List<SeasonCompetionTeam> result = query.list();
		return result;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void saveSeasonCompetionTeam(SeasonCompetion seasonCompetion,
			Team team, int number, String playDay, String playTime) {
		session = sessionFactory.getCurrentSession();
		SeasonCompetionTeam sct = new SeasonCompetionTeam();
		sct.setSeasonCompetion(seasonCompetion);
		sct.setTeam(team);
		sct.setNumber(number);
		sct.setPlayDay(playDay);
		sct.setPlayTime(playTime);
		session.saveOrUpdate(sct);
	}

	public SeasonCompetionTeam findBySeasonCompetionAndTeam(
			SeasonCompetion seasonCompetion, Team team) {
		session = sessionFactory.getCurrentSession();
		SeasonCompetionTeam result = (SeasonCompetionTeam) session.createQuery(
				" from SeasonCompetionTeam sct where sct.seasonCompetion.id="
						+ seasonCompetion.getId() + " and sct.team.id="
						+ team.getId()).uniqueResult();
		return result;
	}

	public void delete(SeasonCompetionTeam sct) {
		session = sessionFactory.getCurrentSession();
		session.delete(sct);
	}

	public List<SeasonCompetionTeam> findByTeam(Team team) {
		session = sessionFactory.getCurrentSession();
		List<SeasonCompetionTeam> result = session.createQuery(
				" from SeasonCompetionTeam sct where sct.team.id="
						+ team.getId()).list();
		return result;
	}

	public SeasonCompetionTeam findBySeasonAndTeam(Season season, Team team) {
		session = sessionFactory.getCurrentSession();
		SeasonCompetionTeam result = (SeasonCompetionTeam) session.createQuery(
				" from SeasonCompetionTeam sct where sct.team.id="
						+ team.getId() + " and sct.seasonCompetion.season.id="
						+ season.getId()).uniqueResult();
		return result;
	}

	public void updateSeasonCompetionTeam(SeasonCompetionTeam sct, int number, String playDay, String playTime) {
		session = sessionFactory.getCurrentSession();
		sct.setNumber(number);
		sct.setPlayDay(playDay);
		sct.setPlayTime(playTime);
		session.merge(sct);
	}

}
